package org.kl.bf.service.workflow;

import java.util.List;
import java.util.Set;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kl.bf.entity.basic.Role;
import org.kl.bf.service.basic.AccountService;
import org.kl.bf.utils.ActivitiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

/**
 * 自定义的Activiti用户管理器
 * 
 * @Author hqm
 * @CreateTime 2014年5月13日 上午8:05:53
 */
@Service
public class CustomUserEntityManager extends UserEntityManager {
	@SuppressWarnings("unused")
	private static final Log logger = LogFactory.getLog(CustomUserEntityManager.class);

	@Autowired
	private AccountService accountService;

	@Override
	public UserEntity findUserById(String loginNo) {
		if (loginNo == null)
			return null;
		try {
			UserEntity userEntity = null;
			org.kl.bf.entity.basic.User bUser = accountService.findUserByLoginNo(loginNo);
			userEntity = ActivitiUtils.toActivitiUser(bUser);
			return userEntity;
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<Group> findGroupsByUser(String loginNo) {
		if (loginNo == null)
			return null;
		Set<Role> roles = accountService.findUserByLoginNo(loginNo).getRoleSet();
		List<Group> gs = null;
		gs = ActivitiUtils.toActivitiGroups(roles);
		return gs;

	}

	@Override
	public List<User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
		throw new RuntimeException("not implement method.");
	}

	@Override
	public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId, String key) {
		throw new RuntimeException("not implement method.");
	}

	@Override
	public List<String> findUserInfoKeysByUserIdAndType(String userId, String type) {
		throw new RuntimeException("not implement method.");
	}

	@Override
	public long findUserCountByQueryCriteria(UserQueryImpl query) {
		throw new RuntimeException("not implement method.");
	}
}
